package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.constants.Constant;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("clueService")
@Transactional
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;

    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;

    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;

    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;

    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;

    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    public int saveCreateClue(Clue clue) {
        return clueMapper.insertClue(clue);
    }

    public List<Clue> queryClueByConditionForPage(Map<String, Object> map) {
        return clueMapper.selectClueByConditionForPage(map);
    }

    public int queryCountOfClueByCondition(Map<String, Object> map) {
        return clueMapper.selectCountOfClueByCondition(map);
    }

    public Clue queryClueById(String id) {
        return clueMapper.selectClueById(id);
    }

    public int updateClueById(Clue clue) {
        return clueMapper.updateClueById(clue);
    }

    public int deleteClueById(String[] id) {
        return clueMapper.deleteClueById(id);
    }

    public Clue queryClueDetailById(String id) {
        return clueMapper.selectClueDetailById(id);
    }

    /**
     * 这个Service实际上处理了很多请求，
     * 1.将查到clue保存到Customer、Contacts表中
     * 2.将查到的clueRemark保存到CustomerRemark、ContactsRemark中
     * 3.将ClueActivityRelation表中的联系批量插入到ContactsActivityRelation表中
     *
     * @param map
     */
    public void saveClueAboutCompany(Map<String, Object> map) {
        //获取clueId
        String clueId = (String) map.get("clueId");
        //获取User
        User user = (User) map.get(Constant.SESSION_USER);
        //获取是否创建tran的值（交易）
        //实际上只有ture和false两个值，但是boolean不能转换成其他类型
        String isCreateTran = (String) map.get("isCreateTran");


        Clue clue = clueMapper.selectClueById(clueId);

        //调用CustomerMapper保存公司相关的Clue
        Customer c = new Customer();
        c.setAddress(clue.getAddress());
        c.setContactSummary(clue.getContactSummary());
        c.setCreateBy(user.getId());
        c.setCreateTime(DateUtils.formateDataTime(new Date()));
        c.setDescription(clue.getDescription());
        c.setId(UUIDUtils.getUUID());
        c.setName(clue.getCompany());
        c.setNextContactTime(clue.getNextContactTime());
        c.setOwner(user.getId());
        c.setPhone(clue.getPhone());
        c.setWebsite(clue.getWebsite());
        customerMapper.insertCustomer(c);

        //调用ContactsMapper保存和联系人想关的Clue
        Contacts co = new Contacts();
        co.setAddress(clue.getAddress());
        co.setDescription(clue.getDescription());
        co.setContactSummary(clue.getContactSummary());
        co.setId(UUIDUtils.getUUID());
        co.setOwner(user.getId());
        co.setCreateBy(user.getId());
        co.setAppellation(clue.getAppellation());
        co.setCreateTime(DateUtils.formateDataTime(new Date()));
        co.setEmail(clue.getEmail());
        co.setJob(clue.getJob());
        co.setFullname(clue.getFullname());
        co.setMphone(clue.getMphone());
        co.setNextContactTime(clue.getNextContactTime());
        co.setCustomerId(c.getId());
        co.setSource(clue.getSource());
        contactsMapper.insertContacts(co);

        //将线索表备注页转到两个表中
        List<ClueRemark> crList = clueRemarkMapper.selectAllClueRemark(clueId);

        //判断线索是否为空
        if (crList != null && crList.size() > 0) {
            //这是客户备注
            CustomerRemark cur = null;
            List<CustomerRemark> curList = new ArrayList<CustomerRemark>();

            //这是联系人备注
            ContactsRemark cor = null;
            List<ContactsRemark> corList = new ArrayList<ContactsRemark>();
            //将查到的线索遍历出来放到CustomerRemark中
            for (ClueRemark cr : crList) {
                cur = new CustomerRemark();
                cur.setCreateBy(cr.getCreateBy());
                cur.setId(UUIDUtils.getUUID());
                cur.setCustomerId(cr.getId());
                cur.setCreateTime(DateUtils.formateDataTime(new Date()));
                cur.setEditBy(cr.getEditBy());
                cur.setEditTime(cr.getEditTime());
                cur.setNoteContent(cr.getNoteContent());
                cur.setEditFlag(cr.getEditFlag());
                curList.add(cur);

                cor = new ContactsRemark();
                cor.setCreateBy(cr.getCreateBy());
                cor.setId(UUIDUtils.getUUID());
                cor.setContactsId(cr.getId());
                cor.setCreateTime(DateUtils.formateDataTime(new Date()));
                cor.setEditBy(cr.getEditBy());
                cor.setEditTime(cr.getEditTime());
                cor.setNoteContent(cr.getNoteContent());
                cor.setEditFlag(cr.getEditFlag());
                corList.add(cor);
            }
            //调用CustomerRemarkMapper保存封装好的备注
            customerRemarkMapper.insertCustomerRemark(curList);
            //调用ContactsRemarkMapper保存封装好的备注
            contactsRemarkMapper.insertContactRemark(corList);
        }

        //调用clue和activity关系表方法查询符合clueId的关系
        List<ClueActivityRelation> relationList = clueActivityRelationMapper.selectClueActivityRelationByClueId(clueId);

        //遍历ClueActivityRelation表中字段的关系
        if (relationList != null && relationList.size() > 0) {
            ContactsActivityRelation caRelation = null;
            List<ContactsActivityRelation> carList = new ArrayList<ContactsActivityRelation>();
            for (ClueActivityRelation car : relationList) {
                caRelation = new ContactsActivityRelation();
                caRelation.setId(UUIDUtils.getUUID());
                caRelation.setContactsId(co.getId());
                caRelation.setActivityId(car.getActivityId());
                carList.add(caRelation);
            }
            //调用ContactActivityRelationMapper保存
            contactsActivityRelationMapper.insertContactsActivityRelationByList(carList);
        }

        //调用TranMapper保存数据
        //要判断前台是否提出了创建交易的请求
        if ("true".equals(isCreateTran)) {
            Tran tran = new Tran();
            String activityId = (String) map.get("activityId");
            tran.setActivityId(activityId);
            tran.setContactsId(co.getId());
            tran.setCreateBy(user.getId());
            tran.setCreateTime(DateUtils.formateDataTime(new Date()));
            tran.setCustomerId(c.getId());
            tran.setExpectedDate((String) map.get("expectedDate"));
            tran.setDescription(co.getDescription());
            tran.setId(UUIDUtils.getUUID());
            tran.setMoney((String) map.get("money"));
            tran.setName((String) map.get("name"));
            tran.setOwner(user.getId());
            tran.setStage((String) map.get("stage"));
            tranMapper.insertTran(tran);

            //添加交易备注
            if (crList != null && crList.size() > 0) {
                TranRemark tr = null;
                List<TranRemark> trList = new ArrayList<TranRemark>();
                for (ClueRemark cr : crList) {
                    tr = new TranRemark();
                    tr.setCreateBy(cr.getCreateBy());
                    tr.setId(UUIDUtils.getUUID());
                    tr.setCreateTime(cr.getCreateTime());
                    tr.setEditBy(cr.getEditBy());
                    tr.setEditTime(cr.getEditTime());
                    tr.setNoteContent(cr.getNoteContent());
                    tr.setTranId(tran.getId());
                    tr.setEditFlag(cr.getEditFlag());
                    trList.add(tr);
                }
                //调用TranRemarkMapper
                tranRemarkMapper.insertTranRemark(trList);
            }
        }

        //根据clueId删除线索表备注的功能
        clueRemarkMapper.deleteClueRemark(clueId);

        //根据clueId删除和市场活动的关系
        clueActivityRelationMapper.deleteClueActivityByClueId(clueId);

        //根据clueId删除clue
        clueMapper.deleteOneClueById(clueId);
    }
}
